<?php

namespace App\Models;

use App\Imports\Batch\SourceCode;
use Askedio\SoftCascade\Traits\SoftCascadeTrait;
use Illuminate\Database\Eloquent\SoftDeletes;

class ProductsSaleModel extends BaseModel
{
    use SoftDeletes;

    protected $fillable   = ['batch_id', 'customer_id', 'sale_num'];

    protected $primaryKey = 'sale_id';

    protected $table      = 'products_sale';

    const MAP_SALE_TYPE = [1 => '流通', 2 => '入市'];

    public function ProductBatch()
    {
        return $this->belongsTo(ProductsBatchModel::class, 'batch_id', 'batch_id');
    }

    public function getSaleTypeAttribute($value)
    {
        return self::MAP_SALE_TYPE[$value] ?? "";
    }

    public function Customer()
    {
        return $this->belongsTo(CustomerModel::class, 'customer_id', 'customer_id');
    }

    /**
     * 创建溯源码
     * @return string
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public static function createSourceCode()
    {
        $sc = app(SourceCode::class);
        /** @var SourceCode $sc */
        return $sc->build();
    }
}
